from django.db import models
from django.conf import settings
from utils.models_save import ModelsSave

# Create your models here.

__all__ = [
    "Account",
    "Email"
]


class Account(models.Model):
    username = models.CharField(unique=True, max_length=32, verbose_name="用户名")
    pwd = models.CharField(max_length=32, verbose_name="密文密码")
    access_token = models.CharField(max_length=320, blank=True, null=True, default=None, verbose_name="TOKEN")
    head_img = models.ImageField(upload_to="account/%Y-%m", blank=True, null=True, verbose_name="个人头像")
    name = models.CharField(max_length=32, blank=True, null=True, verbose_name="姓名")
    sex = models.IntegerField(choices=settings.MODELS_CHOICES['account'], blank=True, null=True, verbose_name="性别")
    age = models.IntegerField(blank=True, null=True, verbose_name="年龄")
    create_time = models.DateTimeField(blank=True, null=True, verbose_name="创建日期")
    update_time = models.DateTimeField(blank=True, null=True, verbose_name="更新日期")
    status = models.IntegerField(choices=settings.MODELS_CHOICES['common'], default=2, verbose_name="状态")

    def save(self, *args, **kwargs):
        old_obj = Account.objects.filter(id=self.id).first()
        rets = ['head_img']
        ModelsSave(old_obj, self, rets).run()  # 删除旧数据
        super(Account, self).save(*args, **kwargs)

    class Meta:
        verbose_name = '用户表'
        verbose_name_plural = '用户表'

    def __str__(self):
        return self.username


# 邮件配置表
class Email(models.Model):
    account = models.ForeignKey('Account', on_delete=True, verbose_name='关联用户表')
    sender = models.CharField(max_length=100, verbose_name='发件人邮箱')
    # receivers = models.CharField(max_length=100,verbose_name='收件人邮箱')
    host_dir = models.CharField(max_length=100, verbose_name='SMTP服务器')
    email_port = models.CharField(max_length=20, default="", verbose_name='SMTP服务器端口')
    username = models.CharField(max_length=100, verbose_name='用户名')
    passwd = models.CharField(max_length=100, verbose_name='密码')
    Headerfrom = models.CharField(max_length=100, verbose_name='发件人头部信息')
    Headerto = models.CharField(max_length=100, verbose_name='收件人头部信息')
    subject = models.CharField(max_length=100, default="", verbose_name='邮件标题')
    create_time = models.DateTimeField(blank=True, null=True, verbose_name="创建日期")
    update_time = models.DateTimeField(blank=True, null=True, verbose_name="更新日期")
    status = models.IntegerField(choices=settings.MODELS_CHOICES['common'], verbose_name="状态")

    class Meta:
        verbose_name = '邮件配置表'
        verbose_name_plural = '邮件配置表'

    def __str__(self):
        return self.username
